<template>
  <div class="detail">
    <headerPhoto>
    </headerPhoto>
    <waveView></waveView>
    <div class="container">
      <div class="detail-header d-flex justify-content-between">
        <div class="left d-flex ">
          <div class="box author me-3">
            <i class="bi bi-person"></i>
            <span class="text-warning">{{ detailObj.author }}</span>
          </div>
          <div class="box time ">
            <i class="bi bi-clock"></i>
            <span class="text-warning">{{ formatDate(String(detailObj.createAt)).split(' ')[0] }}</span>
          </div>
        </div>
        <div class="right">
          <span class="ms-1 rounded" v-for="(item, index) in tagArr" :key="index">
            {{ item }}
          </span>
        </div>
      </div>
      <div @click="goback" class="goback">
        <i class="bi bi-arrow-90deg-left"></i>
      </div>
      <div class="content pt-5">
        <h1 class="title">{{ detailObj.title }}</h1>
        <p class="pt-3" v-html="detailObj.content"></p>
      </div>
      <commentView @publish="publishData" @more="more" :wishArr="wishArr" :hasMoreData />
    </div>
  </div>
</template>

<script setup lang="ts">
import { getArticleDetail, formatDate, imgUrl, getListByArticle, publishArticleCom } from '@/api/index'
import headerPhoto from '@/component/headerPhoto.vue';
import commentView from '@/component/commentView.vue';
import waveView from '@/component/waveView.vue';
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useCounterStore } from '@/stores/counter'
const route = useRoute()
const router = useRouter()
const counter = useCounterStore()
const id = ref<string>()

if (route.query.id) {
  id.value = route.query.id ? String(route.query.id) : ''
}

// 定义详情数据接口
interface IDetail {
  [key: string | number]: string | number
}
// 文章详情数据
const detailObj = ref<IDetail>({})

// 标签数组
const tagArr = ref<string[]>([])
interface Wish {
  id: number;
  user_id: number;
  createAt: string;
  username: string;
  wish_text: string;
  comment: string
  imgUrl: string;
  total: number
}
const wishArr = ref<Wish[]>([])

const page_num = ref<number>(1);
const page_size = ref<number>(10);
const hasMoreData = ref<boolean>(true);

interface Item {
  page_size: number
}
const more = async (data: Item) => {
  if (hasMoreData.value == false) {
    ElMessage({
      showClose: true,
      message: '没有更多数据了~~',
      type: 'warning',
      offset: 100,
      plain: true,
    })
    return
  }
  page_size.value += data.page_size
  await getListByArticleFn()
}
const getListByArticleFn = async () => {
  // console.log('id', id.value)
  await getListByArticle({ article_id: id.value, page_num: page_num.value, page_size: page_size.value })
    .then(res => {
      if (page_size.value > res.data.result.length) {
        hasMoreData.value = false
      }
      // console.log('指定文章评论', res.data)
      if (res.data.code == 200) {
        wishArr.value = res.data.result.map((item: Wish) => {
          return {
            id: item.id,
            user_id: item.user_id,
            createAt: formatDate(item.createAt),
            username: item.username,
            wish_text: item.comment,
            imgUrl: imgUrl(item.imgUrl),
            total: res.data.total
          }
        })
        // console.log(wishArr.value)
      }
    },
      err => {
        console.log(err)
      }
    )
}
getListByArticleFn()

interface publishItem {
  imgUrl: string;
  user_id: number;
  wish_text: string;
  username: string;
}
interface dataObjItem {
  imgUrl: string;
  user_id: number;
  comment: string;
  username: string;
  article_id: string | undefined | number;
}
// 接收子组件传递的数据
const publishData = async (data: publishItem) => {
  // console.log('子组件传递的数据', data)
  const dataObj = {
    imgUrl: data.imgUrl,
    user_id: data.user_id,
    comment: data.wish_text,
    username: data.username,
    article_id: id.value,
  }
  await publishArticleComFn(dataObj)
}

const publishArticleComFn = async (dataObj: dataObjItem) => {
  const token = localStorage.getItem('TTOO');
  if (token) {
    const userStatus = await counter.loginedStatusFn(token)
    // console.log(userStatus)
    if (userStatus) {
      if (dataObj.comment.trim() == '') {
        ElMessage({
          showClose: true,
          message: '你还没写呢~~',
          type: 'warning',
          offset: 100,
          plain: true,
        })
        return false
      }
      await publishArticleCom(dataObj)
        .then(
          res => {
            // console.log(res.data)
            if (res.data.code === 200) {
              ElMessage({
                showClose: true,
                message: '发表成功~~',
                type: 'success',
                offset: 100,
                plain: true,
              })
              getListByArticleFn()
            } else {
              ElMessage({
                showClose: true,
                message: res.data.msg,
                type: 'error',
                offset: 100,
                plain: true,
              })
            }
          },
          err => {
            console.log(err)
          }
        )
    } else {
      ElMessage({
        showClose: true,
        message: '登录已过期~~',
        type: 'error',
        offset: 100,
        plain: true,
      })
      return false
    }
  } else {
    ElMessage({
      showClose: true,
      message: '请先登录~~',
      type: 'error',
      offset: 100,
      plain: true,
    })
    return false
  }
}

// 获取文章详情数据
const getArticleDetailFn = async () => {
  await getArticleDetail({ id: id.value })
    .then(res => {
      // console.log(res.data)
      detailObj.value = res.data[0]
      // console.log("详情", detailObj.value)
      tagArr.value = res.data[0].tag.split("，")
      // console.log("标签", tagArr.value)
    },
      err => {
        console.log(err)
      }
    )
}
getArticleDetailFn()

const goback = () => {
  router.go(-1)
}

</script>

<style lang="less" scoped>
.detail {
  padding-bottom: 20px;
}

.container {
  width: 100%;
  padding: 0 120px;
  transform: translateY(-35px);
}

.detail-header {
  font-family: '楷体';
  font-size: 16px;

  .left {

    .box {
      font-size: 20px;

      .bi-person {
        color: rgb(249, 145, 162);
      }

      .bi-clock {
        color: #dddcf8;
      }

      span {

        color: #fff;
        vertical-align: middle;
        margin-left: 5px;
      }
    }
  }

  .right {
    font-size: 20px;


    span {
      // width: 90px;
      padding: 4px 10px;
      color: #fff;
      text-wrap: wrap;
      background-color: rgba(56, 237, 240, 0.8);
    }
  }

}

.content {
  .title {
    padding-bottom: 20px;
    border-bottom: 2px solid #FFA500;
  }



  &::v-deep pre {
    border-radius: 5px;
    padding: 10px;
    color: #c6c6c6;
    background-color: #000;
  }

  &::v-deep h3 {
    padding: 20px 0 10px;
    border-bottom: 2px solid rgba(255, 165, 0, 0.5);
  }

  &::v-deep h2 {
    padding: 20px 0 10px;
    border-bottom: 2px solid rgba(255, 165, 0, 0.5);
  }

  &::v-deep h4 {
    padding: 20px 0 10px;
    border-bottom: 2px solid rgba(255, 165, 0, 0.5);
  }
}

.goback {
  position: absolute;
  top: 40px;

  i {
    color: #FFA500;
    font-size: 20px;
  }

  .bi {
    &::before {
      font-weight: 600 !important;
    }
  }
}


@media (max-width: 992px) {
  .container {
    padding: 0 50px;
  }
}

@media (max-width: 768px) {
  .container {
    padding: 0 15px;
  }

  .right {
    span {
      font-size: 16px;
    }

  }
}

@media (max-width: 527px) {

  .container {
    transform: translateY(-55px);
  }
}

@media (max-width: 520px) {
  .left {
    display: block !important;
  }

  .goback {
    top: 60px
  }

  .right {
    display: flex;
    flex-wrap: wrap;
    text-align: center;
    align-items: center;
    justify-content: flex-end;

    span {
      vertical-align: middle;
      // display: block;
    }
  }
}

@media (max-width: 450px) {
  .content {
    padding-top: 20px !important;
  }

  .right span {
    transform: translateY(-30px);
  }

}

@media (max-width:433px) {
  .right {
    width: 60%;

    span {
      margin-top: 8px;
    }
  }

}
</style>
